package com.example.demo;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.example.demo.mapper.*;
import com.example.demo.pojo.entity.*;
import com.example.demo.pojo.vo.LabelCountVO;
import io.swagger.models.auth.In;
import lombok.RequiredArgsConstructor;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.*;
import java.util.stream.Collectors;

//
@SpringBootTest
@RequiredArgsConstructor
public class createModule {
    List<QuestionClassification> questionClassifications = new ArrayList<>();
    @Autowired
    private QuestionBankMapper questionBankMapper;
    @Autowired
    private QuestionCategoryMapper questionCategoryMapper;
    @Autowired
    private QuestionClassificationMapper questionClassificationMapper;
    @Autowired
    private QuestionLabelMapper questionLabelMapper;
    @Autowired
    private SubjectFileMapper subjectFileMapper;
    @Autowired
    private QuestionFileMapper questionFileMapper;

    void contactFile(int fileId,List<Integer>questionIdList){
        //将题目的列表储存到文件下
        SubjectFile subjectFile=subjectFileMapper.selectById(fileId);
        String nodeId=subjectFile.getNodeId();
        for(int i=0;i<questionIdList.size();i++){
            QuestionFile questionFile=new QuestionFile();
            questionFile.setNodeId(nodeId);
            questionFile.setQuestionId(questionIdList.get(i));
            questionFile.setFileId(fileId);
            questionFileMapper.insert(questionFile);
        }
    }
    @Test
    void createClassificationLabel(){
        QueryWrapper<QuestionBank>queryWrapper=new QueryWrapper<>();
        List<Integer>idList=questionBankMapper.selectList(queryWrapper).stream().map(QuestionBank::getId).collect(Collectors.toList());
        contactClassification(idList,1);
    }
    //辅助函数--将文件导入的题目与知识点表关联
    void contactClassification(List<Integer>questionIdList,int labelNodeId){
        int totalInsert=0;
        int notLabel=0;
        List<Integer>insertIdList=new ArrayList<>();
        for(int i=0;i<questionIdList.size();i++){
            QuestionBank question=questionBankMapper.selectById(questionIdList.get(i));
            if(question!=null){
                String labelName=question.getLabel();
                if(labelName!=null){
                    List<String> labelNameList = Arrays.stream(labelName.split(";"))
                            .collect(Collectors.toList());
                    List<String>nodeIdList=new ArrayList<>();
                    HashMap<String,Integer>hashMapClassification=new HashMap<>();
                    int saveNumberForTotalNumber=0;
                    int maxTotalNumber=0;
                    //第一层遍历所有的题型
                    for(int j=0;j<labelNameList.size();j++) {
                        QueryWrapper<QuestionLabel>queryWrapper=new QueryWrapper<>();
                        queryWrapper.eq("name",labelNameList.get(j));
                        queryWrapper.likeRight("node_id",1+"-");
                        //进行名字的模糊查找，同时通过根id指定某个学科下的知识点
                        List<QuestionLabel>questionLabelList=questionLabelMapper.selectList(queryWrapper);
                        System.out.println(questionLabelList);
                        //题型存在才进行下一步操作
                        if (!questionLabelList.isEmpty()) {
                            QuestionLabel questionLabel = questionLabelList.get(0);
                            int category=1;
                            int questionLabelId = questionLabel.getId();
                            String nodeId = questionLabel.getNodeId();
                            //逻辑为查找已经储存过的题型关系中如果新添加的属于更深层的或者比先前低，而做出不同的category设置
                            //第二层:根据当前处理的label去判断前面已经储存的label与之的关系
                            for(int k=0;k<nodeIdList.size();k++){
                                String beforeNodeId = nodeIdList.get(k);
                                if(beforeNodeId.startsWith(nodeId)){
                                    System.out.println(nodeId+"----"+beforeNodeId);
                                    category=0; //表示这一题只提供关系
                                }else if(nodeId.startsWith(beforeNodeId)){
                                    int beforeId=hashMapClassification.get(beforeNodeId);
                                    QuestionClassification updateClassification=questionClassificationMapper.selectById(beforeId);
                                    //先前的题只提供关系
                                    UpdateWrapper<QuestionClassification>updateClassificationWrapper=new UpdateWrapper<>();
                                    updateClassification.setClassificationCategory(0);
                                    updateClassificationWrapper.eq("id",updateClassification.getId());
                                    questionClassificationMapper.update(updateClassification,updateClassificationWrapper);
                                }else{ //category设置为2
                                    category=3;
                                    //检查到不一样,需要将前面所有的都进行一次更新---第三层for循环
                                    for(int m=0;m<nodeIdList.size();m++){
                                        String nodeIdFor=nodeIdList.get(m);
                                        int beforeId=hashMapClassification.get(nodeIdFor);
                                        QuestionClassification questionClassification=questionClassificationMapper.selectById(beforeId);
                                        if(questionClassification.getClassificationCategory()!=0) { //不等于0才有意义
                                            //原来为0的不进入处理，主要在处理1上
                                            UpdateWrapper<QuestionClassification> updateClassificationWrapper = new UpdateWrapper<>();
                                            questionClassification.setClassificationCategory(2);
                                            updateClassificationWrapper.eq("id", questionClassification.getId());
                                            questionClassificationMapper.update(questionClassification, updateClassificationWrapper);
                                            saveNumberForTotalNumber = saveNumberForTotalNumber + 1; //表示修改一个为2则将其记录
                                        }
                                    }
                                    System.out.println("检查出对2进行替换--替换的number"+saveNumberForTotalNumber);
                                }
                            }
                            if(saveNumberForTotalNumber>maxTotalNumber){
                                maxTotalNumber=saveNumberForTotalNumber;
                            }
                            int questionBankId = question.getId();
                            QuestionClassification questionClassification = new QuestionClassification();
                            questionClassification.setClassificationCategory(category);
                            questionClassification.setQuestionBankId(questionBankId);
                            questionClassification.setQuestionLabelId(questionLabelId);
                            questionClassification.setNodeId(nodeId);
                            questionClassificationMapper.insert(questionClassification);
                            int classificationId=questionClassification.getId();
                            nodeIdList.add(nodeId);
                            hashMapClassification.put(nodeId,classificationId);
                        }
                    }
                    insertIdList.add(question.getId());
                    totalInsert=totalInsert+1;
                }else{
                    //增添学科处理---当没找到与之相对于的题型，默认划分到根知识点下
                    notLabel=notLabel+1;
                    totalInsert=totalInsert+1;
//                    System.out.println("当前进行的是无label插入--插入的对象是"+question);
                    QuestionClassification questionClassification = new QuestionClassification();
                    questionClassification.setQuestionBankId(question.getId());
                    questionClassification.setQuestionLabelId(labelNodeId);
                    questionClassification.setNodeId("1-");
                    questionClassification.setClassificationCategory(1);
                    questionClassificationMapper.insert(questionClassification);
                    //
                    insertIdList.add(question.getId());
                }
            }
        }
        System.out.println("成功建立联系的题目是"+totalInsert);
        System.out.println("没有题型的题目数量"+notLabel);
        System.out.println("成功进入的bankId"+insertIdList.size());
    }
    int updateClassification(UpdateWrapper<QuestionClassification>updateWrapper,QuestionClassification questionClassification,int categoryId){
        updateWrapper.eq("id",questionClassification.getId());
        questionClassification.setClassificationCategory(categoryId);
        questionClassificationMapper.update(questionClassification,updateWrapper);
        return questionClassification.getId();
    }
    @Test
    void createQuesitonFile(){
        QueryWrapper<QuestionBank>queryWrapper=new QueryWrapper<>();
        List<QuestionBank>questionBankList=questionBankMapper.selectList(queryWrapper);
        List<Integer>idList=questionBankList.stream()
                .map(QuestionBank::getId) // 提取每个 QuestionFile 对象的 id 属性
                .collect(Collectors.toList()); // 收集到新的列表中
        contactFile(1,idList);
    }
}
